<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>RSA+AES 加解密</title>
    <script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
    <script type="text/javascript" th:src="@{/aes/js/JSEncrypt.js}"></script>
    <script type="text/javascript" th:src="@{/aes/js/aes.js}"></script>
    <script type="text/javascript" th:src="@{/aes/js/pad-zeropadding-min.js}"></script>
    <script type="text/javascript" th:src="@{/aes/js/security.js}"></script>
    <script type="text/javascript" th:src="@{/aes/js/random.js}"></script>
</head>
<body>
<script th:inline="javascript"> var ctx = [[@{
    /}]]; </script>
<script type="text/javascript">


    $(function () {
        var datas = {name: "你哈吗"};
        var key = null, token = null;

        var account = {
            "userName": "admin",
            "password": "admin.123"
        }
        //获取秘钥
        $("#get").click(function () {
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: ctx + "interface/getPrivateKey",
                contentType: "application/json",
                data: JSON.stringify(account),
                success: function (result) {
                    $("#servicePublicKey").val(result.publicKey);
                    token = result.token
                    console.log("获取服务端公钥：", result);
                    //通过服务端公钥 ==>> 生成秘钥对
                }
            })
            // $.ajax({
            //     type: 'post',
            //     url: ctx + "yf/reports/exSql",
            //     contentType: "application/json",
            //     success: function (result) {
            //         console.log("获取的数据：", result);
            //         datas = result.rows;
            //     }
            // })
        });


        $("#bt").click(function () {
            // 获取16位随机数,当做aes秘钥key
            var randomStr = randomString(16);
            console.log("生成的aeckey：", randomStr)
            // aes加密
            var word = JSON.stringify(datas), data = Encrypt(word, randomStr);
            console.log("加密的数据：", word)
            // rsa加密
            var encrypt = new JSEncrypt();
            encrypt.setPublicKey($("#servicePublicKey").val());
            var encrypted = encrypt.encrypt(randomStr);
            // 创建json对象
            var json = {
                "requestData": data,
                "encrypted": encrypted
            };

            console.log(json)
            console.log("Authorization: ",token)
            $.ajax({
                type: "POST",
                url: ctx + "interface/receiveData",
                data: JSON.stringify(json),
                headers: {
                    Authorization: token
                },
                dataType: 'json',
                contentType: "application/json",
                success: function (result) {
                    console.log("返回的数据对象:" + JSON.stringify(result));
                    // rsa解密,获取到aes秘钥
                    var decrypt = new JSEncrypt();
                    decrypt.setPublicKey($('#servicePublicKey').val());
                    var aesKey = decrypt.decryptp(result.encrypted);
                    console.log("key:", aesKey)
                    // 用aes秘钥进行解密
                    var resultData = Decrypt(result.requestData, aesKey);
                    console.log("解密的数据：" + resultData);
                }
            });
        });
    });
</script>
<input type="button" id="get" value="获取秘钥"/>
<input type="button" id="bt" value="提交"/>

<h2>服务端公钥：</h2>
<textarea id="servicePublicKey" rows="5" cols="45">
</textarea>
<h2>客户端私钥：</h2>
<textarea id="clientPrivateKey" rows="5" cols="45">
</textarea>
</body>
</html>